import scrapy


class TianqiSpider(scrapy.Spider):
    name = "tianqi"

    async def start(self):
        yield scrapy.Request('https://www.weather.com.cn/', callback=self.parse)

    def parse(self, response):
        # 基本信息
        data = {
            'title': response.xpath('//title/text()').get(),
            'description': response.xpath('//meta[@name="description"]/@content').get(),
            'keywords': response.xpath('//meta[@name="keywords"]/@content').get(),

            # 导航信息
            'navigation': list(zip(
                response.xpath('//div[@class="nav_addr"]//a/text()').getall(),
                response.xpath('//div[@class="nav_addr"]//a/@href').getall()
            )),

            # 焦点新闻
            'focus_news': list(zip(
                response.xpath('//div[@class="focusImg"]//li//b/text()').getall(),
                response.xpath('//div[@class="focusImg"]//li/a/@href').getall(),
                response.xpath('//div[@class="focusImg"]//li/a/img/@src').getall()
            )),

            # 主要新闻
            'main_news': list(zip(
                response.xpath('//div[@class="mainContent"]//h2/a/text()').getall(),
                response.xpath('//div[@class="mainContent"]//h2/a/@href').getall()
            )),

            # 新闻列表
            'news_list': list(zip(
                response.xpath('//div[@class="newsList"]//li/a/text()').getall(),
                response.xpath('//div[@class="newsList"]//li/a/@href').getall()
            )),

            # 气象公告
            'announcements': list(zip(
                response.xpath('//div[@class="announcement"]//h4/a/text()').getall(),
                response.xpath('//div[@class="announcement"]//h4/a/@href').getall(),
                response.xpath('//div[@class="announcement"]//p/text()').getall()
            )),

            # 图片信息
            'images': list(zip(
                response.xpath('//div[@class="hdImgs"]//a/b/text()').getall(),
                response.xpath('//div[@class="hdImgs"]//a/img/@src').getall(),
                response.xpath('//div[@class="hdImgs"]//a/@href').getall()
            )),

            # 雷达图
            'radar': {
                'image': response.xpath('//div[@class="tqld"]//img/@src').get(),
                'link': response.xpath('//div[@class="tqld"]//a/@href').get()
            }
        }
        for item in data.items():
            print(item)


